#include "ui_moviewidget.h"
#include "ui_manager.h"
#include "graphics/avi.h"
#include "graphics/graphics.h"
#include "graphics/graphics_util.h"

#include "audio/audio.h"

#include "util/timer.h"		    // Needs to come in early
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging

#include "script/scripthelper.h"
#include "script/write_lua.h"

#ifdef _ENABLE_LUABIND
#include <luabind/luabind.hpp>
#endif // _ENABLE_LUABIND


using namespace ui;
using namespace script;


UIMovieWidget::UIMovieWidget(UIWidget* pParent,const std::string& Name) :
	UIWidget(pParent, Name),
	m_pClip(NULL)
{
	m_Type = WT_MOVIEWIDGET;

}

UIMovieWidget::~UIMovieWidget()
{
	//UTIL_SAFE_DELETE(m_pClip);
	if (m_pClip)
	{
		GetGraphics()->DestroyMovie(m_pClip);
	}
}

bool		UIMovieWidget::Create(const std::string& Name, const math::Vec2& Pos, const math::Vec2& Size)
{
	UIWidget::Create(Name, Pos, Size);
	// create progress stuff

	return true;
}

bool		UIMovieWidget::Load(LuaPlus::LuaObject& _LObj)
{
	UIWidget::Load(_LObj);	// get all the common stuff loaded
	std::string Clip = ScriptHelper::GetTableString(_LObj, "Clip", "");
	bool		Looping		= ScriptHelper::GetTableBool(_LObj, "Looping", true);
	SetClip(Clip);
	SetLoop(Looping);
	return true;
}
bool		UIMovieWidget::ExportSpecificsToFile(script::WriteLua*	pWL)
{
	if (m_pClip)
	{
		pWL->WritePair("Clip", m_pClip->GetFile());
		pWL->WritePair("Looping", m_pClip->GetLoop());
	}
	return true;
}


bool		UIMovieWidget::Tick(float dt)
{
	UIWidget::Tick(dt);	// tick children, etc
	if (m_pClip)
		m_pClip->Tick();
	return true;
}
void		UIMovieWidget::ResetResources()
{
/*	if (m_pClip)
	{
		std::string	File = m_pClip->GetFile();
		if (m_pClip)
		{
			GetGraphics()->DestroyMovie(m_pClip);
		}
		SetClip(File);
	}
*/	UIWidget::ResetResources();
}

void		UIMovieWidget::SetClip(const std::string& File)
{
	if (m_pClip && m_pClip->GetFile() == File) 
	{
		_LOG(MSG_WARNING, "Trying to re-set the file. Reset instead?");
		Reset();
		return;
	}
	if (m_pClip)
	{
		GetGraphics()->DestroyMovie(m_pClip);
	}
	if (File.empty()) return;	// empty file
	m_pClip = GetGraphics()->LoadMovie(File);
}

void		UIMovieWidget::Reset()
{
	if (m_pClip)m_pClip->Reset();
}

void		UIMovieWidget::SetLoop(bool b)
{
	if (m_pClip)m_pClip->SetLoop(b);
}
bool		UIMovieWidget::IsLooping() const
{
	if (m_pClip) return m_pClip->GetLoop();
	return false;
}
bool		UIMovieWidget::IsPaused() const
{
	if (m_pClip) return m_pClip->GetPause();
	return false;
}


void		UIMovieWidget::PauseClip(bool b)
{
	if (m_pClip) m_pClip->SetPause(b);
}


bool		UIMovieWidget::Render(bool Force)
{
	if (!Force)
	{
		// look for things that could disqualify this one from being rendered
		if (!m_IsVisible)
			return false;	// disable entire widget
		if (m_pManager->GetDragTarget() == this)
			return false;	// will get rendered on top anyways
	}

	if (m_pClip)
	{
//		m_pClip->Render(GetPositionRelativeToParent(), GetSize());
		Graphics::Vertex2D	VertA[6];
		GraphicsUtil::FillQuad(m_Pos, m_Size, m_BaseUV1, m_BaseUV2, m_Color, VertA);
		m_pClip->SetTextureStage(0);
		GetGraphics()->DrawVertex2D(VertA, 6);
	}

	if (m_pManager) m_pManager->WidgetRendered();
	for (ElementList::iterator It = m_Set.begin(); It != m_Set.end(); It++)
		It->second->Render();

	return true;
}

UIWidget*	UIMovieWidget::GetClone()
{
	UIMovieWidget*	pNew = new UIMovieWidget(m_pParent, GetCloneName(m_Name));
	FillCloneInfo((UIWidget*)pNew);
	return pNew;
}


/// register with LUA Bind
void	UIMovieWidget::Register(LuaPlus::LuaState* _pScript)
{
#ifdef _ENABLE_LUABIND
    lua_State * L = _pScript->GetCState ( );

	luabind::module(L)
    [

		luabind::class_<UIMovieWidget, UIWidget >( "SliderWidget" )
		.def( "SetClip",			&UIMovieWidget::SetClip)
		.def( "Reset",				&UIMovieWidget::Reset)
		.def( "SetLoop",			&UIMovieWidget::SetLoop)
		.def( "IsLooping",			&UIMovieWidget::IsLooping)
		.def( "IsPaused",			&UIMovieWidget::IsPaused)
		.def( "PauseClip",			&UIMovieWidget::PauseClip)
	];

#endif // _ENABLE_LUABIND    

}
